#include "passwdnote.h"
#include "ui_passwdnote.h"

extern bool IsLogin;
extern USERINFO UsrInfo;

PasswdNote::PasswdNote(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PasswdNote)
{
    ui->setupUi(this);

    Init();
}

PasswdNote::~PasswdNote()
{
    delete ui;
    delete PsdMenu;
    delete AddNewOne;
    delete DelSelectRow;
}

bool PasswdNote::Init()
{
    InitTheme();

    DataSet.clear();
    CreateMenu();
    InitTableView();

    LoadPasswdNote();

    return true;
}

bool PasswdNote::InitTheme()
{
    ui->lineEdit_Search->setTextMargins(0, 0, ui->pBtn_Search->width(), 0);
    ui->lineEdit_Search->setStyleSheet("QLineEdit{border: 1px solid gray;border-radius: 3px;background:rgb(200, 231, 232);} QLineEdit:hover{border-color:transparent;}");
    ui->pBtn_Search->setStyleSheet("background:transparent;");

    ui->label_PasswdNote_TipInfo->hide();

    return true;
}

bool PasswdNote::InitTableView()
{
    Model = new QStandardItemModel();
    QStringList Titles = QObject::trUtf8("软件名, 账号, 代登账号, 电话号码, 密码, 二级密码").
            simplified().split(",");
    // 设置表头
    Model->setHorizontalHeaderLabels(Titles);
    // 隐藏垂直表头
    ui->tableView->verticalHeader()->hide();
    // 设置显示网格
    ui->tableView->setShowGrid(true);
    ui->tableView->setModel(Model);
    // 设置列宽
    ui->tableView->setColumnWidth(0, 100);
    ui->tableView->setColumnWidth(1, 100);
    ui->tableView->setColumnWidth(2, 100);
    ui->tableView->setColumnWidth(3, 100);
    ui->tableView->setColumnWidth(4, 100);
    ui->tableView->setColumnWidth(5, 90);
    ui->tableView->show();

    ui->tableView->setContextMenuPolicy(Qt::ActionsContextMenu);

    return true;
}

bool PasswdNote::LoadPasswdNote()
{
    if(!IsLogin) {
        ui->label_PasswdNote_TipInfo->show();
        ui->label_PasswdNote_TipInfo->setText("请先登录...");
        return false;
    }
    if(!IsNoteExist())
        CreateNote();

    ui->label_PasswdNote_TipInfo->hide();

    JMySql Jsql;
    if(Jsql.GetPasswdNote(UsrInfo.NickName, DataSet)) {
        for(int i = 0; i < DataSet.size(); i++) {
            // 软件名
            if(DataSet.at(i).SWName == "")
                return false;
            else
                Model->setItem(i, 0, new QStandardItem(DataSet.at(i).SWName));
            // 账号
            if(DataSet.at(i).Account == "")
                return false;
            else
                Model->setItem(i, 1, new QStandardItem(DataSet.at(i).Account));
            // 代登账号
            if(DataSet.at(i).OtherAccount == "")
                Model->setItem(i, 2, new QStandardItem("--"));
            else
                Model->setItem(i, 2, new QStandardItem(DataSet.at(i).OtherAccount));
            // 电话
            if(DataSet.at(i).PhoneNum == "")
                Model->setItem(i, 3, new QStandardItem("--"));
            else
                Model->setItem(i, 3, new QStandardItem(DataSet.at(i).PhoneNum));
            // 密码
            if(DataSet.at(i).Passwd == "")
                Model->setItem(i, 4, new QStandardItem("--"));
            else
                Model->setItem(i, 4, new QStandardItem(DataSet.at(i).Passwd));
            // 二级密码
            if(DataSet.at(i).PayPasswd == "")
                Model->setItem(i, 5, new QStandardItem("--"));
            else
                Model->setItem(i, 5, new QStandardItem(DataSet.at(i).PayPasswd));
        }
    }

    return true;
}

bool PasswdNote::IsNoteExist()
{
    JMySql Jsql;
    QString SQL;
    SQL = QString("show tables from bdm257820317_db");
    if(Jsql.IsDbSheetExist(SQL, UsrInfo.NickName + "_PasswdNote"))
        return true;
    else
        return false;
}

bool PasswdNote::CreateNote()
{
    JMySql Jsql;
    QString SQL;
    SQL = QString("create table if not exists %1_PasswdNote"
                  "(SWName VARCHAR(50) NOT NULL, "
                  "Account VARCHAR(50) NOT NULL, "
                  "OtherAccount VARCHAR(50), "
                  "PhoneNum VARCHAR(50), "
                  "Passwd VARCHAR(50) NOT NULL, "
                  "PayPasswd VARCHAR(50), "
                  "Res1 VARCHAR(50))").
            arg(UsrInfo.NickName);
    if(Jsql.InsertUser(SQL))
        return true;
    else
        return false;
}

bool PasswdNote::CreateMenu()
{
    PsdMenu = new QMenu(this);
    AddNewOne = new QAction("添加", this);
    DelSelectRow = new QAction("删除", this);

    PsdMenu->addAction(AddNewOne);
    PsdMenu->addAction(DelSelectRow);

    return true;
}

//**********************事件************************
void PasswdNote::resizeEvent(QResizeEvent *e)
{
    ui->wgt_PasswdNote_Search->setGeometry(0, 0,
                            this->width(), ui->wgt_PasswdNote_Search->height());
    ui->lineEdit_Search->setGeometry(this->width() - ui->lineEdit_Search->width(), 5,
                            ui->lineEdit_Search->width(), ui->lineEdit_Search->height());
}

// 右击菜单
void PasswdNote::contextMenuEvent(QContextMenuEvent *event)
{
    PsdMenu->move(cursor().pos()); //让菜单显示的位置在鼠标的坐标上
    PsdMenu->show();
}
